﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_KinhTeGiaDinh : System.Web.UI.Page
{
    SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            btnclose.Attributes.Add("onClick", "javascript:history.back(); return false;");
            BindDataToControls();
            drlloainhathue.Enabled = false;
            drlNhamua.Enabled = false;
            txtTongDientichnhamua.Enabled = false;
            tongdientichnhathue.Enabled = false;
        }
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        List<KinhTeGiaDinhClass> list = new List<KinhTeGiaDinhClass>();
        KinhTeGiaDinhClass item;
        foreach (GridViewRow dr in grid.Rows)
        {
            item = new KinhTeGiaDinhClass();
            item.ID = int.Parse(grid.DataKeys[dr.RowIndex].Value.ToString());
            item.ThangNam = ((TextBox)dr.FindControl("txt1")).Text;
            item.NganhBac = ((TextBox)dr.FindControl("txt2")).Text;
            item.HeSo = ((TextBox)dr.FindControl("txt3")).Text;
            list.Add(item);
        }
        item = new KinhTeGiaDinhClass();
        try
        {
            item.ID = list.Last().ID+1;
        }
        catch { item.ID = 1; }
        item.ThangNam = DateTime.Now.Month.ToString()+"/"+DateTime.Now.Year.ToString();
        item.NganhBac = "";
        item.HeSo = "";
        list.Add(item);
        grid.DataSource = list.ToList();
        grid.DataBind();
    }
    private void BindDataToGrid(string val)
    {
        List<KinhTeGiaDinhClass> list = new List<KinhTeGiaDinhClass>();
        KinhTeGiaDinhClass item;
        string[] st = val.Split('$');
        for (int i = 0; i < st.Length - 1; i++)
        {
            string[] st0 = st[i].Split('!');
            item = new KinhTeGiaDinhClass();
            item.ID = i+1;
            item.ThangNam = st0[0];
            item.NganhBac = st0[1];
            item.HeSo = st0[2];
            list.Add(item);
        }
        grid.DataSource = list.ToList();
        grid.DataBind();
    }
    private void BindDataToControls()
    {
        _dbDataContext _db = new _dbDataContext();
        string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"),"gold",128);
        var val = _db.tblKinhTeGiaDinhs.Single(r=>r.MaDoanVien.Trim()==madoanvien);
        txtLuongNam.Text = val.LuongNam.Trim();
        txtNguonKhac.Text = val.NguonKhac.Trim();
        chkNhaThue.Checked = (bool)val.NhaCap;
        chknhamua.Checked = (bool)val.NhaXay;
        if (chkNhaThue.Checked == true)
        {
            drlloainhathue.SelectedValue = val.LoaiNhaThueCap.ToString();
            tongdientichnhathue.Text = val.DienTichThueCap.Trim();
        }
        else
        {
            drlloainhathue.SelectedValue = "-1";
            tongdientichnhathue.Text = "";
        }
        if (chknhamua.Checked == true)
        {
            drlNhamua.SelectedValue = val.LoaiNhaXayMua.ToString();
            txtTongDientichnhamua.Text = val.TongDienTichXayMua.Trim();
        }
        else
        {
            drlNhamua.SelectedValue = "-1";
            txtTongDientichnhamua.Text = "";
        }
        txtDatduoccap.Text = val.DatDuocCap.Trim();
        txtDatTuMua.Text = val.DatTuMua.Trim();
        txtDatsanxuat.Text = val.DatSanXuatKinhDoanh.Trim();
        BindDataToGrid(val.QuanTrinhLuong.Trim());
    }
    protected void chkNhaThue_CheckedChanged(object sender, EventArgs e)
    {
        if (chkNhaThue.Checked == true)
        {
            tongdientichnhathue.Enabled = true;
            drlloainhathue.Enabled = true;
        }
        else
        {
            drlloainhathue.Enabled = false;
            tongdientichnhathue.Enabled = false;
            drlloainhathue.SelectedValue = "-1";
        }
    }
    protected void chknhamua_CheckedChanged(object sender, EventArgs e)
    {
        if (chknhamua.Checked == true)
        {
            drlNhamua.Enabled = true;
            txtTongDientichnhamua.Enabled = true;
        }
        else
        {
            drlNhamua.Enabled = false;
            txtTongDientichnhamua.Enabled = false;
            drlloainhathue.Enabled = true;
            drlNhamua.SelectedValue = "-1";
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        _dbDataContext _db = new _dbDataContext();
        string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"),"gold",128);
        var val = _db.tblKinhTeGiaDinhs.Single(r => r.MaDoanVien.Trim() == madoanvien);
        val.LuongNam = txtLuongNam.Text;
        val.NguonKhac = txtNguonKhac.Text;
        //tinh qua trinh lương
        string st = "";
        TextBox txtthangnam;
        TextBox txtnganh;
        TextBox txtheso;
        foreach (GridViewRow dr in grid.Rows)
        {
            if (grid.Rows[dr.RowIndex].Visible == true)
            {
                txtthangnam = (TextBox)dr.FindControl("txt1");
                txtnganh = (TextBox)dr.FindControl("txt2");
                txtheso = (TextBox)dr.FindControl("txt3");
                st += txtthangnam.Text + "!" + txtnganh.Text + "!" + txtheso.Text;
                st += "$";
            } 
        }
        val.QuanTrinhLuong = st;
        val.LoaiNhaThueCap = int.Parse(drlloainhathue.SelectedValue.ToString());
        val.DienTichThueCap = tongdientichnhathue.Text;
        val.LoaiNhaXayMua = int.Parse(drlNhamua.SelectedValue.ToString());
        val.TongDienTichXayMua = txtTongDientichnhamua.Text;
        val.DatDuocCap = txtDatduoccap.Text;
        val.DatTuMua = txtDatTuMua.Text;
        val.DatSanXuatKinhDoanh = txtDatsanxuat.Text;
        val.NhaXay = chknhamua.Checked;
        val.NhaCap = chkNhaThue.Checked;
        try
        {
            _db.SubmitChanges();
            lblMessage.Text = "Lưu thành công.";
        }
        catch { lblMessage.Text = "Không lưu được."; }
    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        CheckBox chk;
        foreach (GridViewRow dr in grid.Rows)
        {
            chk = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                grid.Rows[dr.RowIndex].Visible = false;
            }
        }
    }
}